package javacompress;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class CompressingFile
{
    
    private String file;
    
    public void doCompressFile(String inFileName)
    {
        try
        {
            File file = new File(inFileName);
            System.out.println(" you are going to gzip the  : "
                    + file + "file");
            FileOutputStream fos = new FileOutputStream(file + ".gz");
            System.out.println(" Now the name of this gzip file is  : "
                    + file + ".gz");
            GZIPOutputStream gzos = new GZIPOutputStream(fos);
            System.out.println(" opening the input stream");
            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            System.out.println("Transferring file from" + inFileName + " to "
                    + file); // + ".gz");
            byte[] buffer = new byte[1024];
            int i;
            while ((i = in.read(buffer)) >= 0)
            {
                gzos.write(buffer, 0, i);
            }
            System.out.println(" file is in now gzip format");
            in.close();
            gzos.close();
        }
        catch (IOException e)
        {
            System.out.println("Exception is" + e);
        }
    }
    
    public CompressingFile(String file)
    {
        this.file = file;
        doCompressFile(file);
        
    }
    
    public CompressingFile()
    {
        // TODO Auto-generated constructor stub
    }
}
